package com.le.tester.live;

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.OpenCVFrameGrabber;

import javax.swing.*;

/**
 * 描述：javavc摄像头获取
 */
public class CameraAccess {
    public static void main(String[] args) throws Exception {

        OpenCVFrameGrabber openCVFrameGrabber = new OpenCVFrameGrabber(0);
        //开始获取摄像头数据
        openCVFrameGrabber.start();
        //新建一个窗口
        CanvasFrame canvasFrame = new CanvasFrame("摄像头");
        canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        canvasFrame.setAlwaysOnTop(true);
        while (true) {
            //摄像头的组件是不是能被显示
            if (!canvasFrame.isDisplayable()) {
                //关闭窗口,停止抓取
                openCVFrameGrabber.stop();
                System.exit(2);

            }
            //获取摄像头图像并放到窗口上显示， 这里的frame是一帧视频图像
            canvasFrame.showImage(openCVFrameGrabber.grab());
            //50ms刷新一次
            Thread.sleep(50);
        }

    }
}
